<?php

/** 
 * @property School_mdl $school
 */
class School extends Admin_Controller {
	
	/**
	 */
	public function __construct() {
		parent::__construct ();
		$this->load->model('School_mdl','school');
	}
	public function index(){
		$schoolList = $this->school->getSchoolList();
		krsort($schoolList,SORT_NUMERIC );
		$temp = array();
		$temp['schoolList'] = $schoolList;
		$this->showView('admin/school/index',$temp);
	} 
	
	public function add(){
		$name = $this->input->post_get('name',TRUE);
		$price = $this->input->post_get('price',TRUE);
		if( empty($name) ){
			$this->apiOutput->errorInput('名称不能为空');
		}
		$data = array();
		$data['name'] = $name;
		$data['firstChargePrice'] = $price;
		$id = $this->school->insert($data);
		
		if ($id) {
			$this->apiOutput->put('status', 1)->put('id', $id)->out();
		}else{
			$this->apiOutput->errorSystem();
		}
	}
	
	public function update(){
		
		$id   = (int)$this->input->post_get('id',TRUE);
		$name = $this->input->post_get('name',TRUE);
		$price = $this->input->post_get('price',TRUE);
		
		if( empty($id) || empty($name) ){
			$this->apiOutput->errorInput('名称不能为空');
		}
		
		$schoolDetail = $this->school->getOneById($id);
		
		$update = array();
		
		if ( $schoolDetail['name'] != $name ){
			$update['name'] = $name;
		}
		
		if ( $schoolDetail['firstChargePrice'] != $price ){
			$update['firstChargePrice'] = $price;
		}
		
		if ( empty($update) ){
			$this->apiOutput->errorInput('信息与原来一致，无需修改');
		}
		
		$where = array('id'=>$id);
		$res = $this->school->update($where, $update);
		
		if ($res) {
			$this->apiOutput->put('status', 1)->out();
		}else{
			$this->apiOutput->errorSystem();
		}
		
	}
}

